class TFCMFlashBang extends TFProjectile;

var bool bCanHitOwner, bHitWater;
var xEmitter Trail;
var() float DampenFactor, DampenFactorParallel;

simulated function Destroyed()
{
    if ( Trail != None )
        Trail.mRegen = false; // stop the emitter from regenerating
	Super.Destroyed();
}

simulated function PostBeginPlay()
{
    Super.PostBeginPlay();

    if ( Level.NetMode != NM_DedicatedServer)
    {
        Trail = Spawn(class'Taskforces.TFCMTrailSmoke', self,, Location, Rotation);
    }

    if ( Role == ROLE_Authority )
    {
        Velocity = Speed * Vector(Rotation) + vect(0,0,50);
        bCanHitOwner = false;
        if (Instigator.HeadVolume.bWaterVolume)
        {
            bHitWater = true;
            Velocity = 0.6*Velocity;
        }
    }
    if ( Instigator != None )
		InstigatorController = Instigator.Controller;

	PlaySound(sound'TFMedia.TFCMFlare',,2*TransientSoundVolume);

    SetTimer(1,false);
}

simulated function tick(float dt)
{
    setrotation(rotator(normal(velocity)));
}

simulated function Timer()
{
    Explode(Location, vect(0,0,1));
}

simulated function Landed( vector HitNormal )
{
    HitWall( HitNormal, None );
}

simulated function ProcessTouch( actor Other, vector HitLocation )
{
    if ( Pawn(Other) != None && (Other != Instigator || bCanHitOwner) )
    {
		Explode(HitLocation, Normal(HitLocation-Other.Location));
    }
}

simulated function HitWall( vector HitNormal, actor Wall )
{
    local Vector VNorm;

    // Reflect off Wall w/damping
    VNorm = (Velocity dot HitNormal) * HitNormal;
    Velocity = -VNorm * DampenFactor + (Velocity - VNorm) * DampenFactorParallel;

    Speed = VSize(Velocity);

    if ( Speed < 20 )
    {
        bBounce = False;
        SetPhysics(PHYS_None);
        //if ( Trail != none )
        //    Trail.mRegen = false;
    }
}

simulated function BlowUp(vector HitLocation)
{
	DelayedHurtRadius(Damage,DamageRadius, MyDamageType, MomentumTransfer, HitLocation );
	if ( Role == ROLE_Authority )
		MakeNoise(1.0);
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
    local TFCMWhiteout w;
    local vector vecPawnView;
    local pawn p;
    local float dist;

    BlowUp(HitLocation);
	PlaySound(sound'TFMedia.TFCMFlareExp',,20*TransientSoundVolume,,2.5*transientsoundradius);
    if ( EffectIsRelevant(Location,false) )
    {
        Spawn(class'Taskforces.TFCMFlash',,, HitLocation, rotator(vect(0,0,1)));
        Spawn(class'Taskforces.TFCMExpSmoke',,, HitLocation, rotator(vect(0,0,1)));
		Spawn(class'Taskforces.TFCMExpSparks',,, HitLocation, rotator(vect(0,0,1)));
        Spawn(ExplosionDecal,self,,HitLocation, rotator(-hitnormal));
    }

    if ( Role==ROLE_Authority )
    {
        foreach dynamicactors(class'pawn',p)
        {
            if (p.lineofsightto(self))
            {
                vecPawnView=p.Location+vect(0,0,1)*p.EyeHeight;
                dist=vSize(p.location-location);
                w=TFCMWhiteout(p.findinventorytype(class'Taskforces.TFCMWhiteout'));

                if (w==none)
                {
                    w=Spawn(class'Taskforces.TFCMWhiteout');
                    w.giveTo(p);
                }

                if ((location-p.location) dot vector(p.getviewrotation())>0.1)
                {
                    w.amount+=8*fmax(1-dist/3072, 0)+1;
                }

                w.amount+=8*fmax(1-dist/512, 0);
            }
        }
    }
    Destroy();
}

defaultproperties
{
     DampenFactor=0.100000
     DampenFactorParallel=0.600000
     Speed=1600.000000
     MaxSpeed=4000.000000
     Damage=50.000000
     DamageRadius=300.000000
     MomentumTransfer=50000.000000
     MyDamageType=Class'taskforces.DamTypeCMAltfire'
     ImpactSound=ProceduralSound'WeaponSounds.PGrenFloor1.P1GrenFloor1'
     ExplosionDecal=Class'XEffects.RocketMark'
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'WeaponStaticMesh.GrenadeMesh'
     Physics=PHYS_Falling
     DrawScale=2.000000
     AmbientGlow=100
     bBounce=True
}
